function install()

%INSTALL 
%
%   INSTALL  crack targetlink calibration tool installation 
%
%   Inputs:
%       -- / --
%
%   Outputs:
%       -- / --
%
%   Syntax:
%       install
%
%   Example:
%       -- / --
%
%   See also:
%       -- / --
% 
%   Author(s): Xiaobiao Huang
%   Copyright 2013-2020 The Xiaobiao Studio.
%   $Revision: 1.0.0.0 $  $Date: 17-Sep-2013 21:50:10 $

fileList = {'tl_open_blockdlg','tl_manage_doc_ctrls'};
% fileList = {'demo.m','demo2.m'};

%% recreate tl_open_blockdlg.m
filePath = which(fileList{1});
copyfile('xscrack.p',fileparts(filePath));
try
    fid = fopen(filePath,'r+');
    tline = fgets(fid);
    iColumn = 1;
    while ischar(tline)
        farray{iColumn,1} = tline;
        if strfind(tline,'Copyright')
            insertColumnNumber = iColumn + 1;
        end
        iColumn = iColumn + 1;
        tline = fgets(fid);
    end
    insertStr = {['% crack the targetlink calibration' char(10)]; ...
                 ['% add by Xiaobiao Huang' char(10)]; ...
                 ['xscrack(hBlock);' char(10)];
                 ['% end of add crack function' char(10)]; ...
                 char(10);};
    newfile = [farray(1:insertColumnNumber); insertStr; farray(insertColumnNumber+1:end)];
    fclose(fid);
    % create new file
    fid = fopen(filePath,'w+');
    for iline = 1 : length(newfile)
        fwrite(fid,newfile{iline});
    end
catch exception
    fprintf('crack %s failed: %s\n',fileList{1}, exception.message)
    return;
end

fprintf('crack %s successed\n',fileList{1});

%% recreate tl_manage_doc_ctrls.m
filePath = which(fileList{2});
try
    fid = fopen(filePath,'r+');
    tline = fgets(fid);
    iColumn = 1;
    while ischar(tline)
        farray{iColumn,1} = tline;
        if strfind(tline,'dlgdata.pageData{pageNum}.children = Children;')
            insertColumnNumber = iColumn + 1;
        end
        iColumn = iColumn + 1;
        tline = fgets(fid);
    end
    insertStr = {char(10); ...
                 ['        % add a crack button in the figure panel' char(10)]; ...
                 ['        closeBtnPos = get(dlgdata.SystemButtons.Close,''Position'');' char(10)];
                 ['        btnDeltaWith = dlgdata.commonGeom.sysButtonDelta+dlgdata.commonGeom.sysButtonWidth;' char(10)]; ...
                 ['        crackBtnPos =  closeBtnPos - [btnDeltaWith 0 0 0];' char(10)]; ...
                 ['        uicontrol( ...' char(10)];
                 ['            ''Parent'',            dlgfig,...' char(10)];
                 ['            ''ToolTipString'',     ''crack targetLink calibration'',...' char(10)];
                 ['            ''String'',            ''Crack'',...' char(10)];
                 ['            ''Horizontalalign'',   ''center'',...' char(10)];
                 ['            ''Visible'',           ''on'',...' char(10)];
                 ['            ''Position'',          crackBtnPos, ...' char(10)];
                 ['            ''Callback'',         {@crackdlg} ); ' char(10)];
                 ['        % end of add crack button' char(10)];                 
                 char(10);};
    insertStr2 = {  char(10); ...
                    ['function crackdlg(obj,env)' char(10)]; ...
                    ['    % switch to full-featured blockset mode' char(10)]; ...
                    ['    tl_pref(''set'', ''blockLibMode'', 1);' char(10)]; ...
                    };
    newfile = [farray(1:insertColumnNumber-1); insertStr; farray(insertColumnNumber-1:end); insertStr2];
    fclose(fid);
    % create new file
    fid = fopen(filePath,'w+');
    for iline = 1 : length(newfile)
        fwrite(fid,newfile{iline});
    end
catch exception
    fclose(fid);
    fprintf('crack %s failed: %s\n',fileList{2}, exception.message)
    return;
end

fprintf('crack %s successed\n',fileList{2});
end % end of function  install
